package com.bosssoft.setp.service;

import com.bosssoft.setp.model.business.course.Course;
import com.bosssoft.setp.model.business.course.CourseType;
import com.bosssoft.setp.pojo.vo.*;
import org.springframework.stereotype.Service;
import xyz.erupt.jpa.dao.EruptDao;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 课程业务层
 *
 * @author clt
 */
@Service
public class CourseService {
    @Resource
    private EruptDao eruptDao;

    /**
     * 获取顶级课程包
     * @return
     */
    public List<PackageVO> getPackages() {
        List<PackageVO> packageVOS = new ArrayList<>();

        List<CourseType> courseTypeList = eruptDao.lambdaQuery(CourseType.class)
                .eq(CourseType::getLevel, 0)
                .list();

        for (CourseType courseType : courseTypeList){
            packageVOS.add(new PackageVO(courseType.getId(),courseType.getName(),"科学实验培训平台"));
        }

        return packageVOS;
    }

    /**
     * 获取某一类别下的子类别和课程表
     * @param courseId
     * @return
     */
    public Object getCourseTypeVO(Long courseId) {
        CourseTypeVO courseTypeVO = new CourseTypeVO();

        CourseType courseType = eruptDao.lambdaQuery(CourseType.class)
                .eq(CourseType::getId, courseId)
                .one();

        courseTypeVO.setCourse_type_id(courseId);
        courseTypeVO.setCourse_type_name(courseType.getName());

        List<Map<String, Object>> subCourseTypes = eruptDao.getJdbcTemplate().queryForList(
                "SELECT id, name FROM setp_course_type WHERE parent_id = ?", courseId);


        List<SubTypeVO> subTypeVOS = new ArrayList<>();
        for (Map<String, Object> row : subCourseTypes){
            subTypeVOS.add(new SubTypeVO(Long.valueOf((Integer) row.get("id")), (String)row.get("name")));
        }
        courseTypeVO.setSub_dir(subTypeVOS);

        List<Map<String, Object>> courses = eruptDao.getJdbcTemplate().queryForList(
                "SELECT id,name,basic_info FROM setp_course WHERE course_type_id = ?", courseId);

        List<SubCourse> subCourseList = new ArrayList<>();
        for (Map<String, Object> row : courses){
            subCourseList.add(new SubCourse(Long.valueOf((Integer) row.get("id")), (String)row.get("name"), (String)row.get("basic_info")));
        }
        courseTypeVO.setCourse_list(subCourseList);
        return courseTypeVO;
    }

    /**
     * 获取课程详情
     * @param courseId
     * @return
     */
    public Object getCourseDetailVO(Long courseId) {
        CoureDetailVO coureDetailVO = new CoureDetailVO();

        coureDetailVO.setCourse_id(courseId);

        List<Attachment> attachmentList = new ArrayList<>();
        List<Map<String, Object>> courseDetails = eruptDao.getJdbcTemplate().queryForList(
                "SELECT attachment_url, attachment_type, allow_download FROM setp_course_detail WHERE course_id = ?", courseId);
        for (Map<String, Object> row : courseDetails){
            attachmentList.add(new Attachment((String)row.get("attachment_url"), (String)row.get("attachment_type"), (Boolean)row.get("allow_download")));
        }

        Course course = eruptDao.lambdaQuery(Course.class)
                .eq(Course::getId, courseId)
                .one();

        if(!course.isVerbalTrickTemplateNull()){
            coureDetailVO.setVerbal_trick_template(course.getVerbalTrickTemplate());
        }else {
            coureDetailVO.setVerbal_trick_template(course.getVerbalTrickTemplateAttachment());
        }

        if(!course.isBusyworkTemplateNull()){
            coureDetailVO.setVerbal_trick_template(course.getBusyworkTemplate());
        }else {
            coureDetailVO.setBusywork_template(course.getBusyworkTemplateAttachment());
        }
        coureDetailVO.setAttachments(attachmentList);
        return coureDetailVO;
    }
}